package cn.edu.njuit.webserver.securityserver.orders;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Description: TODO
 * @Data: 2022/9/19 17:50
 * @Author: guzy
 */
@Service
public class RiderOrderServiceImpl implements RiderOrderService {
    private Log logger = LogFactory.getLog("骑手数据服务");
    private final OrderRepository orderRepository;

    public RiderOrderServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Resource
    OrderQueueService orderQueueService;

    @Override
    public OrderEntity confirmSend(String orderCode) {
        OrderEntity
                orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderStatus(OrderStatus.DELIVING.getName());
            orderEntity.setOrderDeliverConfirmTime(LocalDateTime.now());
            orderEntity.setOrderDeliverName("骑手：快准稳");
            orderEntity.setOrderDeliverPhone("13013013000");
            orderEntity = this.orderRepository.save(orderEntity);
            logger.info(String.format("骑手确认接单：%s", orderCode));
            return orderEntity;
        }
        return null;
    }

    @Override
    public OrderEntity finishDeliver(String orderCode) {
        OrderEntity
                orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderDeliverCompleteTime(LocalDateTime.now());
            orderEntity.setOrderStatus(OrderStatus.DELIVEFINISHED.getName());
            orderEntity = this.orderRepository.save(orderEntity);
            logger.info(String.format("骑手配送完成：%s", orderCode));

            //添加以下代码，配送完成后加入队列4，已完成队列
            this.orderQueueService.sendCompleteMessage(orderEntity.getOrderCode());

            return orderEntity;
        }
        return null;
    }
}
